package com.wctj.api.business.common.service;

import com.wctj.api.base.entity.DdwxPaymentMessageInfo;
import com.wctj.api.business.common.dto.DdwxPaymentMessageInfoDTO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Random;
import java.util.function.Function;

@Slf4j
public abstract class AbstractPayManagerService {
    @Resource
    protected DdwxAdminSetappWxService ddwxAdminSetappWxService;
    @Resource
    protected DdwxPayOrderService ddwxPayOrderService;
    @Resource
    protected DdwxMemberService ddwxMemberService;
    @Resource
    protected DdwxRechargeOrderService ddwxRechargeOrderService;
    @Resource
    protected DdwxMemberMoneyLogService ddwxMemberMoneyLogService;
    @Resource
    protected DdwxAdminSetappMpService ddwxAdminSetappMpService;
    @Resource
    protected DdwxAdminSetappAppService ddwxAdminSetappAppService;
    @Resource
    protected DdwxPaymentCfgInfoService ddwxPaymentCfgInfoService;
    @Resource
    protected DdwxPaymentRecordService ddwxPaymentRecordService;
    @Resource
    protected DdwxPersonalMemberInfoService ddwxPersonalMemberInfoService;
    @Resource
    protected DdwxCorpMemberInfoService ddwxCorpMemberInfoService;
    @Resource
    protected DdwxPaymentMessageInfoService ddwxPaymentMessageInfoService;




    /**
     * java生成随机数字和字母组合10位数
     *
     * @param length [生成随机数的长度]
     * @return
     */
    public String getRandomNum(int length, Function<String, Integer> function) {
        StringBuffer builder = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            builder.append(random.nextInt(10));
        }
        if (function.apply(builder.toString()) > 0){
            return getRandomNum(length,function);
        }
        return builder.toString();
    }


    protected Boolean saveMessage(DdwxPaymentMessageInfoDTO infoDTO) {
        // message db
        DdwxPaymentMessageInfo msgInfo = new DdwxPaymentMessageInfo();
        msgInfo.setBizCode(infoDTO.getBizCode());
        msgInfo.setPayOrderNum(infoDTO.getPayOrderNum());
        msgInfo.setPushStatus(infoDTO.getPushStatus());
        msgInfo.setStatus(infoDTO.getStatus());
        msgInfo.setVersion(0);
        msgInfo.setPushRetryCnt(0);
        msgInfo.setNotifyUrl(infoDTO.getNotifyUrl());
        msgInfo.setRequestBody(infoDTO.getRequestBody());
        msgInfo.setResponseMsg(infoDTO.getResponseMsg());
        msgInfo.setEventType(infoDTO.getEventType());
        msgInfo.setPlatform(infoDTO.getPlatform());
        return ddwxPaymentMessageInfoService.save(msgInfo);
    }

}
